package com.example.controller;

import com.example.pojo.PageBean;
import com.example.pojo.Record;
import com.example.pojo.Result;
import com.example.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    // 获取订单记录列表（可分页、按商家、顾客、分类筛选）
    @GetMapping
    public Result<PageBean<Record>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String sellerName,  // 商家姓名（模糊查询）
            @RequestParam(required = false) String customerName, // 顾客姓名（模糊查询）
            @RequestParam(required = false) String goodsName,    // 商品名称（模糊查询）
            @RequestParam(required = false) Integer cateId       // 商品分类ID（精确查询）
    ) {
        PageBean<Record> pb = recordService.list(pageNum, pageSize, sellerName, customerName, goodsName, cateId);
        return Result.success(pb);
    }

    // 查看订单详情
    @GetMapping("/detail")
    public Result<Record> detail(@RequestParam Integer id) {
        Record record = recordService.findById(id);
        return Result.success(record);
    }

    @GetMapping("/statistics")
    public Result<List<Map<String, Object>>> statistics(
            @RequestParam LocalDateTime startTime,
            @RequestParam LocalDateTime endTime
    ) {
        List<Map<String, Object>> result = recordService.statistics(startTime, endTime);
        return Result.success(result);
    }
}